package com.taiyuan.ty.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taiyuan.ty.entity.PlaceFavorite;
import com.taiyuan.ty.mapper.PlaceFavoriteMapper;
import com.taiyuan.ty.mapper.PlaceMapper;
import com.taiyuan.ty.service.PlaceFavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author : 周桓宇
 * @description : 地点收藏服务实现类
 * @createDate : 2025/3/26 下午8:50
 */
@Service
public class PlaceFavoriteServiceImpl extends ServiceImpl<PlaceFavoriteMapper, PlaceFavorite> implements PlaceFavoriteService {

    @Autowired
    private PlaceMapper placeMapper; // 注入PlaceMapper用于更新favorite_count

    @Autowired
    private PlaceFavoriteMapper placeFavoriteMapper;

    /**
     * 收藏地点
     * @param placeId 地点ID
     * @param userId 用户ID
     * @return 是否成功
     */
    @Transactional
    public boolean favoritePlace(Long placeId, Long userId) {
        PlaceFavorite placeFavorite = new PlaceFavorite();
        placeFavorite.setPlaceId(placeId);
        placeFavorite.setUserId(userId);
        placeFavorite.setCreateTime(new Date());
        int insertResult = placeFavoriteMapper.insert(placeFavorite);
        if (insertResult > 0) {
            // 如果插入成功，更新place表的favorite_count字段
            placeMapper.incrementFavoriteCount(placeId, 1);
            return true;
        }
        return false;
    }

    /**
     * 取消收藏地点
     * @param placeId 地点ID
     * @param userId 用户ID
     * @return 是否成功
     */
    @Transactional
    public boolean unfavoritePlace(Long placeId, Long userId) {
        int deleteResult = placeFavoriteMapper.delete(new QueryWrapper<PlaceFavorite>()
                .eq("place_id", placeId)
                .eq("user_id", userId));
        if (deleteResult > 0) {
            // 如果删除成功，更新place表的favorite_count字段
            placeMapper.incrementFavoriteCount(placeId, -1);
            return true;
        }
        return false;
    }

    /**
     * 获取地点的收藏数量
     * @param placeId 地点ID
     * @return 收藏数量
     */
    public int getPlaceFavoritesCount(Long placeId) {
        return placeFavoriteMapper.selectCount(new QueryWrapper<PlaceFavorite>()
                .eq("place_id", placeId));
    }

    /**
     * 根据用户id获取收藏列表
     * @param userId 用户ID
     * @return 收藏列表
     */
    public List<PlaceFavorite> getFavoritesByUserId(Long userId) {
        return placeFavoriteMapper.selectList(new QueryWrapper<PlaceFavorite>()
                .eq("user_id", userId)
                .orderByDesc("create_time"));
    }

    /**
     * 判断是否已收藏
     * @param placeId 地点ID
     * @param userId 用户ID
     * @return 是否已收藏
     */
    public boolean isFavorite(Long placeId, Long userId) {
        int count = placeFavoriteMapper.selectCount(new QueryWrapper<PlaceFavorite>()
                .eq("place_id", placeId)
                .eq("user_id", userId));
        return count > 0;
    }
}